import * as d3 from 'd3';

export class Util {
  static dateToString(date: Date) {
    return `${date.getFullYear()}_${date.getMonth() + 1}_${date.getDate()}`
  }

  static getDateRange(date: Date) {
    const d1 = new Date(date);
    d1.setHours(0);
    d1.setMinutes(0);
    d1.setSeconds(0);
    d1.setMilliseconds(0);
    const dateStartTime = d1.getTime();

    const d2 = new Date(date);
    d2.setHours(23);
    d2.setMinutes(59);
    d2.setSeconds(59);
    d2.setMilliseconds(999);
    const dateEndTime = d2.getTime();
    return [dateStartTime, dateEndTime]
  }

  static intArrayToBlobUrl(intArray: Int8Array, mimeType: string) {
    const arrayBuffer = new Uint8Array(intArray).buffer;
    const blob = new Blob([arrayBuffer], { type: mimeType });
    const url = URL.createObjectURL(blob)
    return url;
  }

  static darkenColor(color: string, amount: number): string {
    // color '#ffffff' amount [0, +inf]
    const newColor = d3.color(color)!.darker(amount).rgb();
    return `rgb(${newColor.r}, ${newColor.g}, ${newColor.b})`;
  }
}
